<template>
  <div class="model"
       @touchend.self="hideDialog">
    <div class="date-choose">
      <div class="date-choose-top"
           v-if="selectedNewType !== 'type'"
           :class="{'date-choose-top-center': showType < 0}">
        <!-- 点击日期为黑色，箭头左右可点击 -->
        <div class="date-choose-top-year"
             v-if="selectedNewType === 'date'">

          <button class="date-choose-top-year-left"
                  :disabled="canLeft === false"
                  @click="clickLeft()">
            <span></span>
          </button>
          <div class="date-choose-top-year-middle">{{year}}</div>

          <button class="date-choose-top-year-right"
                  :disabled="canRight === false"
                  @touchend="clickRight()">
            <span></span>
          </button>
        </div>
        <!-- 点击类型时日期为灰色，箭头向下 -->
        <div class="date-choose-top-year-disabled"
             v-if="selectedNewType === 'type'">
          <div class="date-choose-top-year-disabled-date">{{year}}-{{selectedMonth}}</div>
          <div class="date-choose-top-year-disabled-img"
               :disabled="canRight === false"
               @click="clickRight()"></div>
        </div>
        <!-- 类型 -->
        <!-- <div class="date-choose-top-type"
             :class="{'date-choose-top-type-selected': income === true}"
             v-if="showType > 0">
          <div class="date-choose-top-type-text"
               :class="{'date-choose-top-type-text-disabled':selectedNewType === 'date'}">类型</div>
          <div class="date-choose-top-type-img"
               :class="{'date-choose-top-type-img-disabled':selectedNewType === 'date'}"></div>
        </div> -->
        <!-- <div class="date-choose-top-returnmoney"
             :class="{'date-choose-top-returnmoney-selected': returnMoney === true}"
             v-if="selectedType !== -1"
             @touchend="clickReturnMoney">退款</div> -->
      </div>
      <!-- 日期选择 -->
      <div class="date-choose-month"
           v-if="selectedNewType === 'date'">
        <div v-for="(item, index) in 12"
             :key="index+'aaaa'"
             class="date-choose-month-list"
             :style="setBackground(item)"
             :class="{specMarginLeft: index === 0 || index === 6 , specMarginTop: index > 5}"
      
             @touchend="clickMonth(item)">
          <div class="date-choose-month-list-month">{{index +1}}月</div>
          <div class="date-choose-month-list-point"
               v-if="item - 1 === month"></div>
        </div>
      </div>
      <!-- 类型选择 -->
      <div class="date-choose-type"
           v-if="selectedNewType === 'type'">
        <div class="date-choose-type-income"
             :class="{'date-choose-type-income-selected':selectedType===1 || income === true}"
      
             @touchend="clickInCome">
          <div></div>收入</div>
        <div class="date-choose-type-returnmoney"
             :class="{'date-choose-type-returnmoney-selected':selectedType===2 || returnMoney === true}"
      
             @touchend="clickReturnMoney">
          <div></div>退款</div>
        <div class="date-choose-type-commision"
             :class="{'date-choose-type-commision-selected':selectedType===3 || commision === true}"
      
             @touchend="clickCommision">
          <div></div>佣金</div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'date-choose',
  props: {
    // 选中的月份
    selectedMonth: {
      type: String,
      default: 1
    },
    // 选中的年
    selectedYear: {
      type: Number,
      default: 1
    },
    // 选中的类型，没有收入退款：-1， 有输入退款，但不选中：0， 收入选中：1，  退款：2
    selectedType: {
      type: Number,
      default: -1
    },
    selectedNewType: {
      type: String,
      default: 'date'
    },
    // 显示组件类型，小于 0 只 显示日期，大于0 显示日期和类型
    showType: {
      type: Number,
      default: 1
    }
  },
  data () {
    return {
      income: this.selectedType === 1,
      returnMoney: this.selectedType === 2,
      commision: this.selectedType === 3,
      canRight: false, // 右边箭头是否可点击
      canLeft: false, // 左边箭头是否可点击
      year: 2017, // 保存 年
      month: 7, // 保存月
      saveMonth: '',
      minYear: 1995 // 最小的年
    }
  },
  created () {
    // 初始化 显示的年  和选中的月
    this.initDate()
    // 检查左边和右边的箭头是否可点击
    this.checkLeft()
    this.checkRight()
  },
  methods: {
    // 点击佣金
    clickCommision () {
      if (this.commision === false) {
        this.commision = true
      } else {
        this.commision = false
      }
      this.income = false
      this.returnMoney = false
      this.clickType()
    },
    px2rem (px) {
      let rem = 100
      return `${px / rem}rem`
    },
    // 点击收入事件
    clickInCome () {
      if (this.income === false) {
        this.income = true
      } else {
        this.income = false
      }
      this.returnMoney = false
      this.commision = false
      this.clickType()
    },
    // 点击退款事件
    clickReturnMoney () {
      if (this.returnMoney === false) {
        this.returnMoney = true
      } else {
        this.returnMoney = false
      }
      this.income = false
      this.commision = false
      this.clickType()
    },
    // 初始化年月
    initDate () {
      let date = new Date()
      // 如果年没有传过来，使用当前年，如果有值，使用传过来的
      this.year = this.selectedYear === -1 ? date.getFullYear() : this.selectedYear
      // 当前的月份
      this.month = date.getMonth()
    },
    // 填充日期，单位数 补0
    zeroMonth (item) {
      return item > 9 ? item : '0' + item
    },
    // 只是单纯的关闭 当前窗口
    hideDialog () {
      this.$emit('hideDialog')
    },
    clickMonth (item) {
      this.saveMonth = item
      // 传回年和月份
      this.$emit('changeMonth', this.year, this.saveMonth)
      this.clickType()
    },
    // 点击选中的月份
    clickType () {
      // 默认为-1，不显示收入 退款
      let selectedType = this.selectedType
      let typeName = ''
      // 收入选中
      if (this.income === true) {
        selectedType = 1
        typeName = '收入'
      }
      // 退款选中
      if (this.returnMoney === true) {
        selectedType = 2
        typeName = '退款'
      }
      // 退款选中
      if (this.commision === true) {
        selectedType = 3
        typeName = '佣金'
      }
      if (this.selectedType !== -1 && this.income === false && this.returnMoney === false && this.commision === false) {
        selectedType = 0
      }
      // 传回年和月份
      this.$emit('changeType', selectedType, typeName)
    },
    // 设置选中的月份的背景
    setBackground (item) {
      let selectedMonth
      if (this.selectedMonth && this.selectedMonth.indexOf('0') === 0) {
        selectedMonth = parseInt(this.selectedMonth.substring(1))
      } else {
        selectedMonth = parseInt(this.selectedMonth)
      }
      if (item === selectedMonth) {
        return {
          color: 'rgba(227, 20, 54, 1)',
          fontSize: this.px2rem(34)
        }
      }
    },
    // 点击右箭头
    clickRight () {
      if (this.checkRight(true) === true) {
        this.year++
      }
    },
    // 点击左箭头
    clickLeft () {
      if (this.checkLeft(true) === true) {
        this.year--
      }
    },
    // 检查右箭头是否可点击
    checkRight (flag = false) {
      let date = new Date()
      // 获取当前年，与现实的年比较，不允许超过当前年
      if (date.getFullYear() <= this.year) {
        this.canRight = false
      } else {
        this.canRight = true
        // flag为真，年已经加过一次，左箭头可点击
        if (flag) {
          this.canLeft = true
        }
      }
      return this.canRight
    },
    // 检查左箭头是否可点击
    checkLeft (flag = false) {
      // 不允许低于最小的年
      if (this.minYear >= this.year) {
        this.canLeft = false
      } else {
        this.canLeft = true
        // flag为真，年已经减过一次，右箭头可点击
        if (flag) {
          this.canRight = true
        }
      }
      return this.canLeft
    }
  },
  components: {
  }
}
</script>


<style lang="stylus">
.model {
  position: absolute;
  top: 100px;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.date-choose {
  width: 100%;
  // height: 265px;
  background: white;
  padding-bottom: 20px;

  .date-choose-top {
    display: flex;
    height: 97px;
    justify-content: space-around;
    align-items: center;

    // margin-left: 54px;

    // margin-right: 98px;
    .date-choose-top-year {
      display: flex;
      height: 97px;
      justify-content: center;
      align-items: center;

      button {
        border: none;
        outline: none;
        background: none;
      }

      .date-choose-top-year-left {
        span {
          background-image: url('~@image/date-left-deep.png');
          background-size: 17px 17px;
          width: 17px;
          background-repeat: no-repeat;
          height: 17px;
        }

        width: 100px;
        height: 100px;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        &:disabled {
          span {
            background-image: url('~@image/date-left.png');
            background-size: 17px 17px;
            width: 17px;
            height: 17px;
            background-repeat: no-repeat;
          }
        }
      }

      .date-choose-top-year-middle {
        font-size: 30px;
        color: rgba(0, 0, 0, 1);
        padding-left: 26px;
        padding-right: 26px;
      }

      .date-choose-top-year-right {
        width: 100px;
        height: 100px;
        display: flex;
        justify-content: flex-start;
        align-items: center;

        span {
          background-image: url('~@image/date-right-deep.png');
          background-size: 17px 17px;
          width: 17px;
          height: 17px;
          background-repeat: no-repeat;
        }

        &:disabled {
          span {
            background-image: url('~@image/date-right.png');
            background-size: 17px 17px;
            width: 17px;
            height: 17px;
            background-repeat: no-repeat;
          }
        }
      }

      .date-choose-top-year-right-deep {
      }
    }

    .date-choose-top-year-disabled {
      display: flex;
      height: 97px;
      justify-content: center;
      align-items: center;

      .date-choose-top-year-disabled-date {
        font-size: 30px;
        color: rgba(153, 153, 153, 1);
      }

      .date-choose-top-year-disabled-img {
        background-image: url('~@image/arrow-down-grey.png');
        background-size: 14px 8px;
        width: 14px;
        height: 8px;
        margin-left: 12px;
        background-repeat: no-repeat;
      }
    }

    .date-choose-top-type {
      font-size: 30px;
      display: flex;
      align-items: center;

      .date-choose-top-type-text {
        color: rgba(51, 51, 51, 1);
      }

      .date-choose-top-type-img {
        background-image: url('~@image/arrow-up.png');
        background-size: 14px 8px;
        width: 14px;
        height: 8px;
        margin-left: 10px;
        background-repeat: no-repeat;
      }

      .date-choose-top-type-text-disabled {
        color: rgba(153, 153, 153, 1);
      }

      .date-choose-top-type-img-disabled {
        background-image: url('~@image/arrow-down-grey.png');
        background-size: 14px 8px;
        width: 14px;
        height: 8px;
        margin-left: 10px;
        background-repeat: no-repeat;
      }
    }

    .date-choose-top-type-selected {
      font-size: 30px;
      color: rgba(230, 27, 60, 1);
    }

    .date-choose-top-returnmoney {
      color: rgba(51, 51, 51, 1);
      font-size: 30px;
    }

    .date-choose-top-returnmoney-selected {
      font-size: 30px;
      color: rgba(230, 27, 60, 1);
    }
  }

  .date-choose-top-center {
    display: flex;
    height: 97px;
    justify-content: center;
    align-items: center;
    // margin-left: 54px;
    // margin-right: 54px;
  }

  .date-choose-month {
    display: flex;
    flex-wrap: wrap;
    padding-top: 12px;

    .date-choose-month-list {
      margin-left: 20px;
      font-size: 28px;
      color: rgba(0, 0, 0, 1);
      text-align: center;
      display: flex;
      align-items: center;
      width: 100px;
      height: 49px;
      justify-content: center;
      border-radius: 50%;
      position: relative;

      .date-choose-month-list-month {
      }
    }

    .specMarginLeft {
      margin-left: 23px;
    }

    .specMarginTop {
      margin-top: 41px;
    }
  }

  .date-choose-type {
    padding-top: 20px;
    // height: 73px;
    display: flex;
    justify-content: space-around;
    font-size: 28px;

    .date-choose-type-income, .date-choose-type-returnmoney, .date-choose-type-commision {
      color: rgba(153, 153, 153, 1);
      border: 1px solid rgba(153, 153, 153, 1);
      border-radius: 100px;
      width: 149px;
      height: 45px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .date-choose-type-income-selected, .date-choose-type-returnmoney-selected, .date-choose-type-commision-selected {
      position: relative;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      border: none;
      background: rgba(227, 20, 54, 0.8);
      border-radius: 100px 2px 100px 100px;

      div {
        position: absolute;
        top: 3px;
        right: 4px;
        width: 10px;
        height: 10px;
        background-image: url('~@image/icon-close-small.png');
        background-size: 10px 10px;
        background-repeat: no-repeat;
      }
    }
  }
}
</style>
